<?php

/**
 * @Author: Wang chunsheng  email:2192138785@qq.com
 * @Date:   2023-05-07 09:35:56
 * @Last Modified by:   Wang chunsheng  email:2192138785@qq.com
 * @Last Modified time: 2023-06-07 16:26:29
 */

namespace App\Admin\Sys;

use Common\AdminController;
use Common\Services\Sys\ProjectService;

class ProjectController extends AdminController
{
    public function add(): array
    {
        $request = $this->request;
        $params = $request->all();
        $title = $params['title'];
        $desc = $params['desc'];

        if (empty($title)) {
            return $this->writeJson(400, null, 'title 不能为空');
        }

        if (empty($desc)) {
            return $this->writeJson(400, null, 'desc 不能为空');
        }

        $member_id = $this->getAttribute('member_id');

        $Res = ProjectService::addProject($title, $desc, $member_id);

        return $this->writeJson(200, $Res, '添加成功');
    }

    public function list(): array
    {
        $request = $this->request;
        $page = $request->input('page') ?? 1;
        $pageSize = $request->input('pageSize') ?? 10;
        $keyWords = $request->input('keyWords') ?? '';

        $Res = ProjectService::listProject($page, $pageSize, $keyWords);

        return $this->writeJson(200, $Res, '获取成功');
    }

    public function edit(): array
    {
        $request = $this->request;
        $params = $request->all();
        $title = $params['title'];
        $desc = $params['desc'];

        if (empty($title)) {
            return $this->writeJson(400, null, 'title 不能为空');
        }

        if (empty($desc)) {
            return $this->writeJson(400, null, 'desc 不能为空');
        }

        $id = $params['id'];
        if (empty($id)) {
            return $this->writeJson(400, null, 'id 不能为空');
        }
        if (empty($title)) {
            return $this->writeJson(400, null, 'title 不能为空');
        }
        if (empty($desc)) {
            return $this->writeJson(400, null, 'desc 不能为空');
        }

        $Res = ProjectService::editProject($id, $title, $desc);

        return $this->writeJson(200, $Res, '编辑成功');
    }

    public function delete(): array
    {
        $request = $this->request;
        $id = $request->input('id');
        if (empty($id)) {
            return $this->writeJson(400, null, 'id 不能为空');
        }

        $Res = ProjectService::deleteProject($id);

        return $this->writeJson(200, $Res, '删除成功');
    }

    public function setBackUrl(): array
    {
        $request = $this->request;
        $id = $request->input('id');
        if (empty($id)) {
            return $this->writeJson(400, null, 'id 不能为空');
        }

        $Res = ProjectService::deleteProject($id);

        return $this->writeJson(200, $Res, '删除成功');
    }
}
